/*
 * @(#) LTRuntimeException.java  1.0  2004-3-29
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Du bf
 * @version 1.0  2004-3-29
 */
public class LTRuntimeException extends RuntimeException {

    private List exceptions = new ArrayList();
    private String messageKey = null;
    private Object[] messageArgs = null;

    public LTRuntimeException() {
        super();
    }

    public LTRuntimeException(String key, Object[] args) {
        super();
        this.messageKey = key;
        this.messageArgs = args;
    }

    public LTRuntimeException(String key) {
        super();
        this.messageKey = key;
        this.messageArgs = null;
    }

    public LTRuntimeException(Throwable rootCause, String key, Object[] args) {
        super(rootCause);
        this.messageKey = key;
        this.messageArgs = args;
    }

    public LTRuntimeException(Throwable rootCause) {
        super(rootCause);
    }

    public List getExceptions() {
        return exceptions;
    }

    public void addException(LTException ex) {
        exceptions.add(ex);
    }

    public void setMessageKey(String key) {
        this.messageKey = key;
    }

    public String getMessageKey() {
        return messageKey;
    }

    public void setMessageArgs(Object[] args) {
        this.messageArgs = args;
    }

    public Object[] getMessageArgs() {
        return messageArgs;
    }

    public String getMessage() {
        String myMessage = super.getMessage();
        if (myMessage == null) myMessage = "LTCM Exception - Error Key : " + this.messageKey;
        return myMessage;
    }

    public String[] getMessages() {
        Throwable[] throwables = this.getThrowables();
        String[] msgs = new String[throwables.length];
        for (int i = 0; i < throwables.length; i++) {
            msgs[i] = throwables[i].getMessage();
        }
        return msgs;
    }

    public Throwable[] getThrowables() {
        List list = new ArrayList();
        Throwable throwable = this;
        while (throwable != null) {
            list.add(throwable);
            throwable = throwable.getCause();
        }
        return (Throwable[]) list.toArray(new Throwable[list.size()]);
    }
}
